//系统配置-用户管理-角色管理
App.util.ns('Admin.Access.User');
Admin.Access.User.RoleManage = function(dataTable){
    //无限极折叠
    var printer = {
        rootKeyLength: 100,
        lists: [],
        frame: $([
            '<table class="table table-hover no-margin data-table">',
            '   <thead>',
            '       <tr>',
            '           <th style="text-align:center;">角色</th>',
            '           <th width="80px" style="text-align:center;">选择</th>',
            '       </tr>',
            '   </thead>',
            '   <tbody>',
            '   </tbody>',
            '</table>'
        ].join('')),
        flush: function(data, selected){
            this.rootKeyLength = 100;
            this.lists = [];
            this.printTr(data);
            var tbody = this.frame.find('tbody');
            tbody.children().each(function(){
                $(this).remove();
            });
            tbody.append(this.getEmptyTr());
            for(var i in this.lists){
                var tr = this.lists[i];
                var id = tr.attr('data-id');
                var spaceCount = (id.length - this.rootKeyLength)/2;
                var html = '<span>';
                for(var i=0;i<spaceCount;i++){
                    html += '&nbsp;&nbsp;&nbsp;&nbsp;';
                }
                html += '</span>';
                $(html).prependTo(tr.find('td').first());
                tr.appendTo(tbody);
            }
            tbody.find('input[name="role_id"][value="'+selected+'"]').prop('checked', true);
            tbody.find('.down').each(function(){
                $(this).click(function(){
                    var selfId = $(this).parent().parent().attr('data-id');
                    tbody.find('tr').each(function(){
                        if($(this).attr('data-id') === selfId){
                            $(this).find('.down').hide();
                            $(this).find('.up').show();
                            return ;
                        }
                        if($(this).attr('data-parent-id') === selfId){
                            $(this).show();
                            if($(this).hasClass('has-sub')){
                                $(this).find('.up').hide();
                                $(this).find('.down').show();
                            }
                        }
                    });
                });
            });
            tbody.find('.up').each(function(){
                $(this).click(function(){
                    var selfId = $(this).parent().parent().attr('data-id');
                    tbody.find('tr').each(function(){
                        if($(this).attr('data-id') === selfId){
                            $(this).find('.up').hide();
                            $(this).find('.down').show();
                            return ;
                        }
                        if($(this).attr('data-id').indexOf(selfId) === 0){
                            $(this).hide();
                        }
                    });
                });
            });
        },
        getEmptyTr: function(){
            return $([
                '<tr data-id="" data-parent-id="">',
                '   <td style="text-align:left;">',
                '       无',						
                '   </td>',
                '   <td style="text-align:center;">',
                '       <label class="label-radio inline">',
                '           <input type="radio" name="role_id" value="0">',
                '           <span class="custom-radio"></span>',
                '       </label>',
                '   </td>',
                '</tr>'
            ].join(''));
        },
        printTr: function(data){
            for(var key in data){
                this.lists.push(this.getTr(data[key]));
                if(data[key]['sub']){
                    this.printTr(data[key]['sub']);
                }
            }
        },
        getTr: function(data){
            var roleId = data['role_id_code']+'';
            var parentId = data['parent_id_code'];
            if(roleId.length < this.rootKeyLength){
                this.rootKeyLength = roleId.length;
            }
            var tr = $([
                '<tr data-id="'+roleId+'" data-parent-id="'+parentId+'">',
                '   <td style="text-align:left;">',
                '       <a href="javascript:void(0)" title="展开" class="down" style="display:none;">',
                '           <i class="fa fa-plus"></i>',
                '       </a>',
                '       <a href="javascript:void(0)" title="收起" class="up" style="display:none;">',
                '           <i class="fa fa-minus"></i>',
                '       </a>',
                '       <a href="javascript:void(0)" class="none" style="display:none;">',
                '           <i class="fa fa-ban"></i>',
                '       </a>',
                '       '+data['name'],
                '   </td>',
                '   <td style="text-align:center;">',
                '       <label class="label-radio inline">',
                '           <input type="radio" name="role_id" value="'+data['role_id']+'">',
                '           <span class="custom-radio"></span>',
                '       </label>',
                '   </td>',
                '</tr>'
            ].join(''));
            if(data.sub_count > 0){
                tr.addClass('has-sub');
                tr.find('.up').show();
            }else{
                tr.find('.none').show();
            }
            return tr;
        }
    };
    var win = new App.Win({name: 'operation_manage'});
    win.printer = printer;
    win.setContent(win.printer.frame);
    win.contentContainer.append('<input type="hidden" name="user_id">');
    //设置按钮
    win.setBtns([(function(){
        var cancelBtn = $('<a class="btn btn-danger btn-xs m-right-sm">取消</a>');
        cancelBtn.click(function(){
            win.hide();
        });
        return cancelBtn;
    })(), (function(){
        var saveBtn = $('<a class="btn btn-success btn-xs">保存</a>');
        saveBtn.click(function(){
            dataTable.postData({
                url: 'changeRole',
                alert: '保存成功！',
                reload: false,
                data: {
                    user_id: win.contentContainer.find('input[name="user_id"]').val(),
                    role_id: (function(){
                        var role_id = 0;
                        win.contentContainer.find('input[name="role_id"]').each(function(){
                            if($(this).prop('checked')){
                                role_id = $(this).val();
                            }
                        });
                        return role_id;
                    })()
                },
                after: function(){
                    win.hide();
                }
            });
        });
        return saveBtn;
    })()]);
    win.hide();
    return win;
};